package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.infantschool.R;
import com.android.infantschool.bean.AuthCodeBean;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.bean.ChangPswBean;
import com.android.infantschool.model.mvp.presenter.AuthCodePresenter;
import com.android.infantschool.model.mvp.presenter.AuthCodePresenterImpl;
import com.android.infantschool.model.mvp.presenter.ChangePswPresenter;
import com.android.infantschool.model.mvp.presenter.ChangePswPresenterImpl;
import com.android.infantschool.model.mvp.view.AuthCodeView;
import com.android.infantschool.model.mvp.view.ChangePswView;
import com.android.infantschool.util.ToastUtil;
import com.android.infantschool.util.ZhengZe;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\20 0020 15:32
 * @class describe
 */
public class ForgetPswActivity extends BaseActivity implements AuthCodeView, ChangePswView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.ed_user_phone)
    public EditText edUserPhone;
    @BindView(R.id.ed_phone_code)
    public EditText edPhoneCode;
    @BindView(R.id.btn_get_code)
    public Button btnGetCode;
    @BindView(R.id.ed_new_password)
    public EditText edNewPassword;
    @BindView(R.id.ed_affirm_password)
    public EditText edAffirmPassword;
    @BindView(R.id.btn_relogin)
    public Button btnRelogin;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;

    private CountDownTimer countDownTimer = null;
    private AuthCodePresenter authCodePresenter;
    private ChangePswPresenter changePswPresenter;

    public static Intent newIntent(Activity activity) {
        Intent intent = new Intent(activity, ForgetPswActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_psw);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        authCodePresenter = new AuthCodePresenterImpl(this);
        changePswPresenter = new ChangePswPresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("忘记密码");
    }

    @OnClick({R.id.img_back, R.id.btn_get_code, R.id.btn_relogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
            case R.id.btn_get_code:
                final String phones = edUserPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phones)) {
                    ToastUtil.showShortToast(this, "请输入手机号");
                    return;
                }
                if (!ZhengZe.isMobileNO(phones) || phones.length() != 11) {
                    ToastUtil.showShortToast(this, "请输入正确的手机号");
                    return;
                }
                Map<String, String> maps = new HashMap<>();
                maps.put("tel", phones);
                authCodePresenter.getAuthCode(maps);
                break;
            case R.id.btn_relogin:
                final String phone = edUserPhone.getText().toString().trim();
                final String code = edPhoneCode.getText().toString().trim();
                final String newPsw = edNewPassword.getText().toString().trim();
                final String enterPsw = edAffirmPassword.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showShortToast(this, "请输入手机号");
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    ToastUtil.showShortToast(this, "请输入验证码");
                    return;
                }
                if (TextUtils.isEmpty(newPsw)) {
                    ToastUtil.showShortToast(this, "请输入新密码");
                    return;
                }
                if (TextUtils.isEmpty(enterPsw)) {
                    ToastUtil.showShortToast(this, "请输入确认密码");
                    return;
                }

                if (!ZhengZe.isMobileNO(phone) || phone.length() != 11) {
                    ToastUtil.showShortToast(this, "请输入正确的手机号");
                    return;
                }

                if (code.length() != 4) {
                    ToastUtil.showShortToast(this, "请输入正确的验证码");
                    return;
                }
                Log.e(TAG, "new:" + newPsw + ",enter:" + enterPsw);
                if (!newPsw.equals(enterPsw)) {
                    ToastUtil.showShortToast(this, "两次输入的密码不一致");
                    return;
                }
                Map<String, String> map = new HashMap<>();
                map.put("tel", phone);
                map.put("key", code);
                map.put("newPassword", newPsw);
                Log.e(TAG, "map:" + map.toString());
                changePswPresenter.postChangePsw(map);
                break;
        }
    }

    @Override
    public void errorAuthCode(String error) {
        btnGetCode.setEnabled(true);
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successAuthCode(BaseBean baseBean) {
        AuthCodeBean codeBean = (AuthCodeBean) baseBean;
        if (codeBean.getCode().equals("0")) {
            ToastUtil.showShortToast(this, "发送成功");
            btnGetCode.setEnabled(false);
            if (countDownTimer != null) {
                countDownTimer.cancel();
                countDownTimer = null;
            }
            countDownTimer = new CountDownTimer(60 * 1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    btnGetCode.setText(millisUntilFinished / 1000L + "");
                }

                @Override
                public void onFinish() {
                    btnGetCode.setText("获取验证码");
                    btnGetCode.setEnabled(true);
                }
            };
            countDownTimer.start();
        } else {
            btnGetCode.setEnabled(true);
            ToastUtil.showShortToast(this, codeBean.getMsg());
        }
    }

    @Override
    public void errorChangePsw(String error) {
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successChangePsw(BaseBean baseBean) {
        ChangPswBean changPswBean = (ChangPswBean) baseBean;
        if (changPswBean.getCode().equals("0")) {
            ToastUtil.showShortToast(this,
                    "修改成功");
            this.finish();
        } else {
            ToastUtil.showShortToast(this,
                    changPswBean.getMsg());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
        if (changePswPresenter != null) {
            changePswPresenter.onDestroy();
            changePswPresenter = null;
        }
        if (authCodePresenter != null) {
            authCodePresenter.onDestroy();
            authCodePresenter = null;
        }
    }

}
